/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "ProjectManager.hpp"

Project* ProjectManager::NewProject( const wxString& cmdname, const wxString& chatlink, const wxString& filename )
{
	Project* p = new Project(cmdname, chatlink, filename);
	return p;
}

Project* ProjectManager::LoadProject( const wxString& filename, HtmlEdit* ctrl )
{
	std::wifstream stream(filename.mb_str());
	if ( stream.is_open() && stream.good() )
	{
		// Get ChatLink First
		std::wstring chatlink;
		std::wstring cmdname;
		
		// First Line is ProjectName
		std::getline(stream, cmdname);

		// Second Line is the chatlink
		std::getline(stream, chatlink);
		
		// Now get the content and stream it to the wxTextCtrl
		while ( !stream.eof() )
		{
			std::wstring line;
			std::getline<wchar_t>(stream, line);
			ctrl->WriteText(line);
			ctrl->WriteText("\n");
		}
		stream.close();

		Project* p = new Project(wxString(cmdname), wxString(chatlink), wxString(filename));
		return p;
	}
	return NULL;
}

bool ProjectManager::SaveProject( Project* project, HtmlEdit* ctrl )
{
	std::wofstream stream(project->GetFileName().wc_str());

	if ( stream.is_open() && stream.good() )
	{
		stream << project->GetCommandName().wc_str();
		stream << std::endl;
		stream << project->GetChatLinkName().wc_str();
		stream << std::endl;
		stream << ctrl->GetValue().wc_str();
		stream.close();
		return true;
	}

	return false;
}

bool ProjectManager::SaveProjectAs( Project* project, const wxString& filename, HtmlEdit* ctrl )
{
	project->m_filename = filename;
	return this->SaveProject(project, ctrl);
}